Decision Making, Arrays-1, 2, and 3 dimensional and Output



Welcome to the 3rd installment in my C/C++ tutorial series. In this tutorial we will cover decision making (if statements), arrays and output forms.

Decision Making with "if"


I hope all of you have heard of a conditional statement (not necessarily in programming). Examples of this include the popular "if it is not raining, we will go on a picnic". This sets up a situation where if something is true (or false as the case may be) then the next statement occurs. This is represented in C and C++ as the following:

if (  )
{
	statement 1;
	statement 2;
	...
}

Using the if statement with the conditional "if it is not raining, we will go on a picnic" gives us:

if (not raining)
{
go on a picnic;
}

In C/C++, this is expressed as follows:

if (!raining)
{
	go on a picnic;	
}

The exclamation point "!" is what is called the logical negation operator or the NOT operator. Basically this: "!raining" translates to "not raining". If statements can be without the opening and closing curly braces if (and only if) there is only 1 statement to be executed.
So, you can save space and say this:
if (!raining)
	go on a picnic;

It's really that easy. But, you have to watch out for some common mistakes that are also easy to make. For example, let's say you wanted to say that if it's not raining, you could go on a picnic and go bowling and go to a movie. The following example is _NOT_ the way to do this:

if (!raining)
	go on a picnic;
	go bowling;
	go to a movie;

This doesn't mean what you might think it does.
Remember this: tabs do not tell your program how to run. They are just there to help you read your code clearly.
This code says if it is raining, then we will not go on a picnic, but we will go to a movie and go bowling. It is equal to the following example:

if (!raining)
	go on a picnic;
go bowling;
go to a movie;

How you tab your code is up to you and your compiler won't care where you choose to put them 90% of the time.
If you wanted to say that you will do all three things if it's not raining, try this one out:

if (!raining)
{
	go on a picnic;
	go bowling;
	go to a movie;
}
So you see what a difference curly braces can make with if statements. But, what if you wanted to say "if it is not raining, we will go on a picnic, if it is, then we will play a board game"? You could use 2 if statements, like this:

if (!raining)
	go on a picnic;
if (raining)
	play a board game;

Ewww... that doesn't look very good at all, does it? So we use what is called an if-else statement.

if (!raining)
	go on a picnic;
else
	play a board game;

That works just fine. You can think of else statements by using the word "otherwise" as in the explanation for the above: "If it is not raining, I will go on a picnic. Otherwise, I will play a board game." Else statements follow curly braces the same way if statements do. They can be used without curly braces only if there is only 1 statement using the else statement. The previous example could also be written as follows:

if (!raining)
{
	go on a picnic;
}
else
{
	play a board game;
}

Which ever you prefer, though the first is much shorter. Next, we have the conditional equality operator (big name). This just means that it is an operator to test whether two things are equal. This operator is simply 2 equals signs "==". They are used in the code snippet that follows. If you are confused about the i and j part, check back with tutorial 2 on data storage types.

int i=5, j=5;

if (i == j)
	printf("i equals j\n");

This is a test of whether i is equal to j. Obviously, since we put the numbers in there, we already knew they would be equal. However, this is just to show you how this is done so that it becomes easier later. Make sure you distinguish the conditional equality operator "==" from the assignment operator "=".

If you have any questions about this, drop me an email. Make sure you understand this thoroughly before continuing. This is critical to most (probably all) complex programs. But for now, on with the teaching.

Arrays:



Picture this: You run a bakery that makes cookies. You sell 20 different kinds of cookies and you want to write a program to keep track of the number of cookies you have on hand at the moment. Given what we've done so far, you might start by writing something that looks like this:

int ChocolateChip;
int PeanutButter;
int Snickerdoodle;
...
int OatmealRaisin;

Now, for only 20 kinds of cookies, this might not be so bad, but imagine if you doubled your cookie inventory, added cakes, breads, pies, candies and you can quickly see that keeping track of all those variables individually quickly becomes an impossible problem. Here's where an array comes in. Arrays are simply no more than a bunch of variables that are grouped together like a list. Say we took those 20 cookies from before, added 10 cakes, 15 pies and 25 kinds of candy. You might be saying "that's a lot of data to keep track of!" Well, you'd be right, but we can do it easily, like this:

int cookies[20];
int cakes[10];
int pies[10];
int candies[25];

That's all there is to storing all that data. You might be surprised at what that looks like, but take a look at the numbers in there, and then look at the number of each kind of pastry that we sell. All the brackets-[]- do are tell the compiler that we want to set up a list of this type, in this case, integers.
Now, as for how we actually GET to the data in here. The number of the first cookie we have can be found by using
cookies[0]
up through to the last cookie quantity at
cookies[19]
. This is very important. Arrays ALWAYS start at 0 and go up to 1 less than the number used to define them. In this case, it went up to 9. The first element in an array is element number 0. But suppose instead of ten variables we need 10 rows of 10 variables for a table (chart) that shows data. This is where 2 dimensional arrays come into play. They're not that much different from 1 dimensional arrays. Instead of cookies and pastries this time, we'll just use simple integers:
int number[10][10];

This will give us integer variables from number[0][0] to number[9][9]. Just remember that, like the 1 dimensional arrays, the 2 and 3 dimensional ones always begin with element 0. Also, two dimensional variables are referenced like this number[row][col]. This may be confusing at first, but just try to hold on and experiment a little to clarify it. By now I think you've guessed what 3 dimensional arrays look like in their declarations. Yup, you guessed it:
int number[10][10][10];

Variable data structure of integers 10 columns across, 10 rows heigh and 10 layers thick. You could conceivably do higher dimensions of arrays, but seeing as how there is no physical representation of these, they aren't generally used a whole lot. But, feel free to experiment if you want to, especially if you could use them.

Output:



Next, output. Something very important to getting information back to the user. What good is a program if the user can't find out the answer to what the program calculates? This is where you will use the printf/scanf codes I gave you in tutorial 2, so if you have it, dig it out from under your programming book and take a look at it. For those of you who skipped over that tutorial (shame on you), here it is:

TypePrintf/Scanf Code
integer%i
short int%hi
long int%li
unsigned int%u
char%c
float%f
double%f

This is only the basics of the type codes, there are a whole heck of a lot more that I didn't include for simplicity's sake. However, this should do everything you need for now. Here is an example of how to print a value of a variable to the screen.

int i=5;
printf ("i = %i", i);

As you can see, the printf/scanf code is inserted inside the quotes wherever you wish the value of the variable to be shown. After the quotes, put a comma and then the variable names in order of when they are printed out. This only is needed when you are printing more than 1 variable, as shown below:

int i=5, j=6, k=7;
printf ("i = %i, j = %i, k = %i", i, j, k);

If that doesn't make sense, I'm not sure how else to explain it. Tell me exactly what you don't understand and I'll clarify it for you if I can.

That's all for this tutorial, thanks for reading it and being interested in the world of programming. Following are the exercises for this tutorial, so warm up your compiler and get ready to test your knowledge.

Tasks:
1. Create a program which accepts five numbers from the user and stores them to an array. Then print out which ever ones you want back to the user.

2. Create a program to have the user input two numbers and test them to determine which is greater or if they are equal. Tell the user which one is greater and the value of both numbers.

3. Create a table of values where the user can pick a value by entering the 1 and 2 numbers in a 2 dimensional array declaration to find the answer. Print this answer back to the user.

That's it for tutorial 3. If this didn't make sense, reread it and the previous ones until you are clear on everything. Or just send me an email and I'll try to clarify it for you.